package cn.lijiajia3515.cairo.system.modules.area;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 行政区划api
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/area")
class AreaApi {
	private final AreaService areaService;

	public AreaApi(AreaService areaService) {
		this.areaService = areaService;
	}

	/**
	 * 区域查询
	 *
	 * @param param 查询参数
	 * @return 区域 集合
	 */
	@PostMapping("/find")
	List<Area> find(@RequestBody @Valid @Validated FindAreaParam param) {
		return areaService.find(param);
	}

	/**
	 * 根据 id 查询区域
	 *
	 * @param id id
	 * @return 区域对象
	 */
	@GetMapping("/find_id/{id}")
	Optional<Area> findById(@PathVariable String id) {
		return areaService.findById(id, Collections.emptyMap());
	}

	/**
	 * 根据 id 查询子级区域
	 *
	 * @param id id
	 * @return 子级区域
	 */
	@GetMapping("/find_subs/{id}")
	List<AreaTree> find(@PathVariable String id) {
		return areaService.subs(id);
	}
}
